#!/usr/bin/env python2
# coding: utf-8


import unittest

from uuid import UUID

from zstack import Client
from zstack.controller import system

ADMIN_USERNAME = 'admin'
ADMIN_PASSWORD = 'password'
ZSTACK_API_URL = 'http://192.168.1.115:8080/zstack/api'

NOT_EXIST = 'not_exist'
# 115服务器上host参数
HOST_UUID = 'c5970195297f4e8da98fbd3e001e8645'
HOST_CPU = 80
HOST_MEMORY = 33504464896
HOST_AVAILABLE_CPU = 74
HOST_AVAILABLE_MEMORY = 27062013952
# 115服务器上主存储服务器参数
PRIMARY_UUID = '8bd3d148c9b245c698ac04146fd9c18a'
PRIMARY_CAPACITY = 2736767602688
PRIMARY_AVAILABLE_CAPACITY = 2175965563392
# 115服务器上备份服务器参数
BACKUP_UUID = '48a1019169004434a78968d18dc09000'
BACKUP_CAPACITY = 2736767602688
BACKUP_AVAILABLE_CAPACITY = 2265095941632


class TestSystem(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.client = Client(ADMIN_USERNAME, ADMIN_PASSWORD, ZSTACK_API_URL)
        cls.client.login()

    def test_query_host(self):
        # 查询host相关信息
        hs = system.query_host(self.client)
        self.assertTrue(all([isinstance(UUID(h.get('uuid')), UUID) for h in hs]))
        self.assertTrue(all([isinstance(h.get('totalCpuCapacity'), (type(HOST_CPU))) for h in hs]))
        self.assertTrue(all([isinstance(h.get('totalMemoryCapacity'), (type(HOST_MEMORY))) for h in hs]))
        self.assertTrue(all([isinstance(h.get('availableCpuCapacity'), (type(HOST_AVAILABLE_CPU))) for h in hs]))
        self.assertTrue(all([isinstance(h.get('availableMemoryCapacity'), (type(HOST_AVAILABLE_MEMORY))) for h in hs]))
        # 与不存在信息进行比较
        self.assertNotIn(NOT_EXIST, [host.get('uuid') for host in hs])
        self.assertNotIn(NOT_EXIST, [host.get('totalCpuCapacity') for host in hs])
        self.assertNotIn(NOT_EXIST, [host.get('totalMemoryCapacity') for host in hs])
        self.assertNotIn(NOT_EXIST, [host.get('availableCpuCapacity') for host in hs])
        self.assertNotIn(NOT_EXIST, [host.get('availableMemoryCapacity') for host in hs])
        # 查询不存在信息
        self.assertNotIn(HOST_UUID, [host.get(NOT_EXIST) for host in hs])
        self.assertNotIn(HOST_CPU, [host.get(NOT_EXIST) for host in hs])
        self.assertNotIn(HOST_MEMORY, [host.get(NOT_EXIST) for host in hs])
        self.assertNotIn(HOST_AVAILABLE_CPU, [host.get(NOT_EXIST) for host in hs])
        self.assertNotIn(HOST_AVAILABLE_MEMORY, [host.get(NOT_EXIST) for host in hs])

    def test_query_primary_storage(self):
        # 查询主存储相关信息
        ss = system.query_primary_storage(self.client)
        self.assertTrue(all([isinstance(UUID(s.get('uuid')), UUID) for s in ss]))
        self.assertTrue(all([isinstance(s.get('totalCapacity'), type(PRIMARY_CAPACITY)) for s in ss]))
        self.assertTrue(all([isinstance(s.get('availableCapacity'), type(PRIMARY_AVAILABLE_CAPACITY)) for s in ss]))
        # 与不存在信息进行比较
        self.assertNotIn(NOT_EXIST, [storage.get('uuid') for storage in ss])
        self.assertNotIn(NOT_EXIST, [storage.get('totalCapacity') for storage in ss])
        self.assertNotIn(NOT_EXIST, [storage.get('availableCapacity') for storage in ss])
        # 查询不存在信息
        ss = system.query_primary_storage(self.client)
        self.assertNotIn(PRIMARY_UUID, [s.get(NOT_EXIST) for s in ss])
        self.assertNotIn(PRIMARY_CAPACITY, [s.get(NOT_EXIST) for s in ss])
        self.assertNotIn(PRIMARY_AVAILABLE_CAPACITY, [s.get(NOT_EXIST) for s in ss])

    def test_query_backup_storage(self):
        # 查询备份存储相关信息
        ss = system.query_backup_storage(self.client)
        self.assertTrue(all([isinstance(UUID(s.get('uuid')), UUID) for s in ss]))
        self.assertTrue(all([isinstance(s.get('totalCapacity'), type(PRIMARY_CAPACITY)) for s in ss]))
        self.assertTrue(all([isinstance(s.get('availableCapacity'), type(PRIMARY_AVAILABLE_CAPACITY)) for s in ss]))
        # 与不存在信息进行比较
        self.assertNotIn(NOT_EXIST, [s.get('uuid') for s in ss])
        self.assertNotIn(NOT_EXIST, [s.get('totalCapacity') for s in ss])
        self.assertNotIn(NOT_EXIST, [s.get('availableCapacity') for s in ss])
        # 查询不存在的信息
        self.assertNotIn(BACKUP_UUID, [s.get(NOT_EXIST) for s in ss])
        self.assertNotIn(BACKUP_CAPACITY, [s.get(NOT_EXIST) for s in ss])
        self.assertNotIn(BACKUP_AVAILABLE_CAPACITY, [s.get(NOT_EXIST) for s in ss])


if __name__ == '__main__':
    unittest.main()
